package cn.stylefeng.guns.modular.robot.cmd.impl;

import cn.stylefeng.guns.modular.robot.cmd.abs.AbstractCmd;
import cn.stylefeng.guns.modular.robot.utils.RobotUtils;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.RestTemplate;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class CmdGPT extends AbstractCmd {

    @Resource
    private RestTemplate restTemplate;

    @Override
    public void exec(Update update, TelegramLongPollingBot bot) {
        String text = update.getMessage().getText();
        text = text.substring(3);
        ResponseEntity<ResponseData> responseEntity = restTemplate.getForEntity("http://47.76.177.255:9420/custom/open/chat/completions?input=" + text + "&apiKey=2", ResponseData.class);
        ResponseData responseData = responseEntity.getBody();
        Map<String,String> map = (Map<String, String>) responseData.getData();
        RobotUtils.sendText(update.getMessage().getChatId(), map.get("content"),bot);
    }

    @Override
    public String getCmdName() {
        return "GPT";
    }

    @Override
    public String getDescribe() {
        return "GPT                           智能问答\n";
    }
}
